import sys
import os
import random

_str_ = '#define RANDOM_KEY'

def get_random_key(in_file):
    line_count = 0
    with open(in_file, 'r', encoding='utf-8') as f:
        line = f.readline()
        while line:
            index = line.find(_str_)
            if index != -1:
                print("old line:" + line)
                break
            line = f.readline()
            line_count += 1

    with open(in_file, 'r+', encoding='utf-8') as f:
        # line = f.readline()
        # while line:
        #     index = line.find(_str_)
        #     if index != -1:
        #         print("old line:" + line)
        #         line = '#define RANDOM_KEY 12345'
        #         f.seek(f.tell())
        #         f.write(line)
        #         print("new line:" + line)
        #         break
        #     line = f.readline()
        while line_count > 0:
            f.readline()
            line_count -= 1
        f.seek(f.tell())
        random_key = random.randint(0x10000000,0xFFFFFFFF)
        line = '#define RANDOM_KEY ' + hex(random_key)
        f.write(line)
        print("new line:" + line)

if __name__ == "__main__":
    get_random_key(sys.argv[1])
